<template>
  <div class="app-container">
    <el-card class="filter-container">
      <div>
        <i class="el-icon-search" />
        <span>筛选搜索</span>
        <div class="filter-btn">
          <el-button
            icon="el-icon-search"
            type="primary"
            size="small"
            @click="handleSearchList()"
          >
            查询
          </el-button>
          <el-button
            icon="el-icon-setting"
            size="small"
            @click="handleResetSearch()"
          >
            重置
          </el-button>
        </div>

      </div>
      <div style="margin-top: 25px">
        <el-form :inline="true" :model="listQuery" size="small">
          <el-row>

            <el-col :sm="12" :lg="6">
              <el-form-item label="应用名：">
                <el-input v-model="listQuery.applicationName" placeholder="应用名" clearable />
              </el-form-item>
            </el-col>

            <el-col :sm="12" :lg="6">
              <el-form-item label="IP：">
                <el-input v-model="listQuery.ip" placeholder="IP" clearable />
              </el-form-item>
            </el-col>

            <el-col :sm="12" :lg="6">
              <el-form-item label="请求地址：">
                <el-input v-model="listQuery.uri" placeholder="输入请求地址" clearable />
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :sm="12" :lg="12">
              <el-form-item label="请求时间：">
                <el-date-picker
                  v-model="exceptionTime"
                  value-format="yyyy-MM-dd"
                  type="daterange"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期"
                  :default-time="['08:00:00']"
                />
              </el-form-item>
            </el-col>
          </el-row>

        </el-form>
      </div>
    </el-card>

    <el-card class="table-container">
      <el-table
        v-loading="listLoading"
        element-loading-text="加载中"
        :data="list"
      >
        <el-table-column align="center" label="用户id" property="userId" />
        <el-table-column align="center" fixed label="应用名" property="applicationName" />
        <el-table-column align="center" fixed label="请求地址" property="uri" />
        <el-table-column align="center" label="请求方法" property="method" />
        <el-table-column align="center" label="UserAgent" property="userAgent" />
        <el-table-column align="center" label="IP" property="ip" />
        <el-table-column align="center" label="异常名" property="exceptionName" />
        <el-table-column align="center" label="异常时间" property="exceptionTime" />
        <el-table-column align="center" label="创建时间" property="createTime" />

        <el-table-column label="操作" align="center" class-name="operate-cell">
          <template slot-scope="scope">

            <el-button
              size="mini"
              type="success"
              @click="view(scope.row)"
            >查看
            </el-button>

          </template>
        </el-table-column>

      </el-table>

      <pagination v-show="total>0" :total="total" :page.sync="listQuery.pageNum" :limit.sync="listQuery.pageLimit" @pagination="getList" />
    </el-card>

    <el-dialog
      title="异常详情"
      :visible.sync="viewVisible"
      center
    >
      <el-form>
        <el-form-item label="用户id：">
          <el-input
            v-model="exception.userId"
          />
        </el-form-item>
        <el-form-item label="用户名：">
          <el-input
            v-model="exception.applicationName"
          />
        </el-form-item>
        <el-form-item label="异常内容">
          <el-input
            v-model="exception.body"
          />
        </el-form-item>
        <el-form-item label="请求地址">
          <el-input
            v-model="exception.uri"
          />
        </el-form-item>
        <el-form-item label="请求方法">
          <el-input
            v-model="exception.method"
          />
        </el-form-item>
        <el-form-item label="user_agent">
          <el-input
            v-model="exception.userAgent"
          />
        </el-form-item>
        <el-form-item label="ip">
          <el-input
            v-model="exception.ip"
          />
        </el-form-item>
        <el-form-item label="异常名">
          <el-input
            v-model="exception.exceptionName"
          />
        </el-form-item>
        <el-form-item label="异常类全名">
          <el-input
            v-model="exception.exceptionClassName"
          />
        </el-form-item>
        <el-form-item label="异常类文件">
          <el-input
            v-model="exception.exceptionFileName"
          />
        </el-form-item>
        <el-form-item label="异常方法名">
          <el-input
            v-model="exception.exceptionMethodName"
          />
        </el-form-item>
        <el-form-item label="异常方法所在行">
          <el-input
            v-model="exception.exceptionLineNumber"
          />
        </el-form-item>
        <el-form-item label="异常栈轨迹">
          <el-input
            v-model="exception.exceptionStackTrace"
            rows="6"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="异常根message">
          <el-input
            v-model="exception.exceptionRootCauseMessage"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="异常message">
          <el-input
            v-model="exception.exceptionMessage"
            rows="6"
            type="textarea"
          />
        </el-form-item>
        <el-form-item label="异常时间">
          <el-input
            v-model="exception.exceptionTime"
          />
        </el-form-item>
        <el-form-item label="创建时间">
          <el-input
            v-model="exception.createTime"
          />
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="viewVisible = false">关 闭</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>

import {
  getList,
  getById
} from '@/api/log/exception-log'

import Pagination from '@/components/Pagination'

const defaultListQuery = {
  pageNum: 1,
  pageLimit: 10,
  sorts: '-exceptionTime'
}

export default {
  components: { Pagination },

  data() {
    return {
      responseTime: null,

      startTime: null,

      listQuery: Object.assign({}, defaultListQuery),
      list: [],
      total: 0,

      listLoading: true,
      loading: false,

      viewVisible: false,
      exception: {}
    }
  },
  watch: {
    exceptionTime: function(newValue) {
      if (newValue != null) {
        this.listQuery.startExceptionTime = newValue[0]
        this.listQuery.endExceptionTime = newValue[1]
      } else {
        this.listQuery.startExceptionTime = null
        this.listQuery.endExceptionTime = null
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      this.listLoading = true
      getList(this.listQuery).then(response => {
        this.list = response.data.records
        this.total = response.data.total
        this.listLoading = false
      })
    },
    handleSearchList() {
      this.listQuery.pageNum = 1
      this.getList()
    },
    handleResetSearch() {
      this.listQuery = Object.assign({}, defaultListQuery)
    },
    view(exception) {
      getById(exception.id).then(res => {
        this.exception = res.data
        this.viewVisible = true
      })
    }
  }
}
</script>

<style scoped>
</style>
